package com.jiantong.work.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Maps;
import io.terminus.common.utils.JsonMapper;
import lombok.Data;
import org.assertj.core.util.Strings;

import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
import java.util.Map;

/**
 * Created by niufei on 2017/4/11.
 *
 */
@Data
public class BreedReportsForm implements Serializable{

    private static final ObjectMapper objectMapper = JsonMapper.nonEmptyMapper().getMapper();


    private static final long serialVersionUID = 1515395741452847780L;

    /**
     * 主键id
     */
    private Long id;

    /**
     * 申报id
     */
    private Long reportId;

    /**
     * 申报号
     */
    private String reportNumber;

    /**
     * 养殖场id
     */
    private Long breedFactoryId;

    /**
     * 养殖场名称
     */
    private String breedFactoryName;

    /**
     * 是否是散户
     */
    private Integer isScale;

    /**
     * 养殖场地址
     */
    private String breedFactoryAddress;

    /**
     * 养殖场所属区域
     */
    private String breedFactoryTown;

    /**
     * 货主
     */
    private String owner;

    /**
     * 手机号码
     */
    private String ownerMobile;

    /**
     * 申报点id
     */
    private Long applyReportId;

    /**
     * 申报点名称
     */
    private String applyReportName;

    /**
     * 养殖类型 1、猪 2、牛 3、羊 4、家禽（申报类型）
     */
    private Integer breedType;

    /**
     * 单位
     */
    private Integer unit;

    /**
     * 数量
     */
    private Integer quantity;

    /**
     * 屠宰场id
     */
    private Integer butcherFactoryId;

    /**
     * 屠宰场名称
     */
    private String butcherFactoryName;

    /**
     * 收货地址
     */
    @JsonIgnore
    private String receiveAddress;

    private Map<String , Object> receiveAddressMap;

    private String receiveAddressValue;

    /**
     * 是否留在本市 -1、外市 1、本市
     */
    private String isToLocal;

    /**
     * 运输工具号牌
     */
    private String transportNumber;

    /**
     * 官方兽医id
     */
    private Long vetUserId;

    /**
     * 官方兽医名称(就是当前登录人)
     */
    private String vetUserName;

    /**
     * 无害化处理ID
     */
    private Long harmlessId;

    /**
     * 无害化处理编号
     */
    private String harmlessNumber;

    /**
     * 检疫合格数
     */
    private Integer qualifiedQuantity;

    /**
     * 检疫不合格数量
     */
    private Integer unqualifiedQuantity;

    /**
     * 一般处理数量
     */
    private Integer generalTreatmentQuantity;

    /**
     * 无害化处理数量
     */
    private Integer harmlessTreatmentQuantity;

    /**
     * 检疫合格证编号
     */
    private String quarantineCertNumber;

    /**
     * 申报日期
     */
    private Date reportDate;

    /**
     * 是否有效-1 1
     */
    private Integer deleteFlag;

    private String remark;


    public void setReceiveAddress(String receiveAddress){
        this.receiveAddress = receiveAddress;

        if(Strings.isNullOrEmpty(receiveAddress)){
            this.receiveAddressMap = Maps.newHashMap();
        }else {
            try {
                this.receiveAddressMap = objectMapper.readValue(receiveAddress, new TypeReference<Map<String , Object>>() {});
            } catch (IOException e) {
                // this is fuck exception
            }
        }
    }

    public void setReceiveAddressMap(Map<String , Object> receiveAddressMap){
        this.receiveAddressMap = receiveAddressMap;

        if(receiveAddressMap == null || receiveAddressMap.isEmpty()){
            this.receiveAddress = null;
        }else {
            try {
                this.receiveAddress = objectMapper.writeValueAsString(receiveAddressMap);
            } catch (JsonProcessingException e) {
                // this is fuck exception
            }
        }
    }
}
